// pages/home/home.js
let {
  getHomeData,
  recommendBlank,
  homeHisearch,
} = require("../../api/home")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiperHeight: 200,
    //轮播图？
    imgs: [
      "https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/51b3f561b860023b9739c9ef2ad4be9e.jpg",
      "https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/d80ff6ad33796a3c5eb7434fe5fb00a0.jpg?f=webp&w=1080&h=540&bg=F5EEE6",
      "https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/51b3f561b860023b9739c9ef2ad4be9e.jpg",
      "https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/51b3f561b860023b9739c9ef2ad4be9e.jpg",
      "https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/51b3f561b860023b9739c9ef2ad4be9e.jpg",
    ],
    current: 0,
    category: [{
        img: "/image/01.png",
        text: "天官赐福"
      },
      {
        img: "/image/02.png",
        text: "魔道祖师"
      },
      {
        img: "/image/03.png",
        text: "苍兰诀"
      },
      {
        img: "/image/04.png",
        text: "盗墓笔记"
      },
      {
        img: "/image/05.png",
        text: "哈利波特"
      },
      {
        img: "/image/06.png",
        text: "动漫画集"
      },
      {
        img: "/image/07.png",
        text: "青春小说"
      },
      {
        img: "/image/08.png",
        text: "每日上新"
      },
      {
        img: "/image/09.png",
        text: "盲盒机"
      },
      {
        img: "/image/10.png",
        text: "次元服装"
      },
    ],
    conIndex: 0,
    swiper_height: "200px",
    goodsLists: [{
        name: "精选优品",
        title: "商品推荐",
        lists: [
          // {
          // id:"",
          //   img:"https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/d9d7f31258a1f84a277918cecab14aaa.jpg?f=webp&w=516&h=420",
          //   name:"小米手机",
          //   price:29,
          //   priceOld:129
          // }
        ]
      },
      {
        name: "IP周边",
        title: "探索黑科技",
        lists: []
      },
      {
        name: "青春小说",
        title: "数码电器",
        lists: []
      },
    ]
  },

  // 商品 选项卡切换
  setConIndex(event) {
    // console.log(event.currentTarget.dataset.id);
    let index = event.currentTarget.dataset.id
    this.setData({
      conIndex: index,
    })

    this.getNavData(index);
  },
  // 轮播图商品切换
  setSwiperGoodsIndex(event) {
    // console.log("触发效果", event.detail.current);
    this.setData({
      conIndex: event.detail.current,
    })
    this.autoHeight();
  },

  // 获取 手机 家电 数据
  getNavData(index) {
    if (this.data.goodsLists[index].lists.length == 0) { //没有数据才请求数据
      homeHisearch({
        query: this.data.goodsLists[index].name
      }).then((data) => {
        // console.log(data);
        let list_v2 = data.data.list_v2;
        let listsArr = [];
        list_v2.forEach(item => {
          listsArr.push({
            id: item.body.product_id,
            img: item.body.image,
            name: item.body.name,
            price: item.body.price,
            priceOld: item.body.market_price
          })
        })
        // console.log("数据列表==>", listsArr);
        // this.setData({
        //   ["goodsLists[" + index + "].lists"]: listsArr
        // })


      }).then(() => {
        this.autoHeight();
      })
    }



  },

  // 计算内容高度
  autoHeight() {

    let {
      conIndex
    } = this.data;

    wx.createSelectorQuery()
      .select('#end' + conIndex).boundingClientRect()
      .select('#start' + conIndex).boundingClientRect().exec(rect => {
        // console.log(rect);
        let _space = rect[0].top - rect[1].top; //end高度-start高度=差值
        _space = _space + 'px';

        this.setData({
          swiper_height: (217 * 20) + "px"
        });

      })

  },

  // 自定义方法
  // 轮播图指示点
  setSwiperIndex(event) {
    // console.log("触发效果", event.detail.current);
    // this.setData 修改data中数据,数据修改后更新视图
    this.setData({
      current: event.detail.current
    })
  },

  // 获取图片高度
  bindloadImg(e) {
    let {
      height,
      width
    } = e.detail;
    // 获取硬件系统信息
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          swiperHeight: res.windowWidth * 0.95 / width * height
        })
      }
    })
  },

  // 发起GET请求
  getInfo() {
    wx.request({
      url: 'https://www.lofter.com/market/homePage/feed/detail.json?feedId=-1',
      method: "GET",
      success: (res) => {
        let dataArr = res.data.data.feedRecommends
        let newDataArr = dataArr.filter(item => item.type == 0)
        // console.log(newDataArr);
        this.setData({
          ["goodsLists[0].lists"]: newDataArr
        })
      }
    })
  },

  // 发起POST请求
  postInfo() {
    wx.request({
      url: 'https://api.lofter.com/v1.1/yitou/madr',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: "POST",
      data: {
        category: "EXPLOREBANNER",
        extJson: '{"domainId":"-1000"}'
      },
      success: (res) => {
        //广告轮播图
        this.setData({
          imgs: res.data[0].ads
        })

        if (res.data) {
          setTimeout(function () {
              wx.hideLoading()
            },
            1000)
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    wx.showLoading({
      title: '加载中...',
    })

    this.postInfo()
    this.getInfo()


    // getHomeData().then(data => {
    //   let sections = data.data.data.sections;
    // })

    // recommendBlank().then(data => {
    //   // console.log(data);
    //   let recom_list = data.data.recom_list;
    //   let listsArr = [];
    //   recom_list.forEach(item => {
    //     listsArr.push({
    //       id: item.action.path,
    //       img: item.image_url,
    //       name: item.name,
    //       price: item.price,
    //       priceOld: item.market_price
    //     })
    //   })
    //   this.setData({
    //     "goodsLists[0].lists": listsArr
    //   })
    // }).then(() => {
    //   this.autoHeight();
    // })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    // console.log("onReady");

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // console.log("onShow");
    this.autoHeight();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    // console.log("onHide");

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})